package awesome_pool

import "sync"

type Queue struct {
	Master  *QueueNode
	Head    *QueueNode
	Foot    *QueueNode
	size    int
	maxSize int
	sync.Mutex
}
type QueueNode struct {
	Data   interface{}
	Status int
	Node   *QueueNode
}

func NewQueue(size int) *Queue {
	return &Queue{
		Master:  nil,
		size:    0,
		maxSize: size,
	}
}
func (q *Queue) Push(key, data interface{}) {
	q.Lock()
	defer q.Unlock()
	d := q.Master
	if q.size > q.maxSize {
		return
	}
	if d == nil {
		q.Master = &QueueNode{
			Data:   data,
			Status: 0,
			Node:   nil,
		}
		q.size = 1
		return
	}
	for d.Node != nil {
		d = d.Node
	}
	d.Node = &QueueNode{
		Data:   data,
		Status: 0,
		Node:   nil,
	}
	q.size++
}

/**
从头部取出一个 具体操作 ->
获取master节点
把Node节点的值赋给 master
*/
func (q *Queue) Pop() QueueNode {
	q.Lock()
	defer q.Unlock()
	head := *q.Master
	*q.Master = *q.Master.Node
	q.size--
	return head
}
func (q *Queue) Size() int {
	return q.size - 1
}
